package org.abettor.angleworm.ui;

import java.io.IOException;

import org.abettor.angleworm.i18n.Translator;
import org.abettor.angleworm.util.Configuration;
import org.abettor.util.Environment;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.graphics.Rectangle;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Group;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Monitor;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

/**
 * angleworm登录界面
 * @author shawn
 */
public class MsnLoginDialog extends LoginDialog
{
	private static Translator translator = null;
	private static final int WIDTH = 300;
	private static final int HEIGHT = 220;
	private boolean loginSuccess = false;
	private String account;
	private Display display = Display.getDefault();
	private Shell loginShell = null;
	private Label labelBanner = null;
	private Group group = null;
	private Label labelUser = null;
	private Combo comboUser = null;
	private Label labelAt = null;
	private Combo comboDomain = null;
	private Label labelPass = null;
	private Text textPass = null;
	private Composite optionComposite = null;
	private Button checkRemember = null;
	private Combo comboStatus = null;
	private Composite buttonComposite = null;
	private Button buttonLogin = null;
	private Button buttonExit = null;
	private Button buttonAdvance = null;

	/**
	 * 显示登录窗口
	 */
	public void show()
	{
		createLoginShell();
		loginShell.open();
		while(! loginShell.isDisposed())
		{
			if(! display.readAndDispatch())
			{
				display.sleep();
			}
		}
	}

	/**
	 * 绘制登录窗口主界面
	 */
	private void createLoginShell()
	{
		try
		{
			translator = Translator.getInstance();
			if(translator == null)
			{
				translator = Translator.initialize("en_US");
			}
		}
		catch (IOException e)
		{
			e.printStackTrace();
			return;
		}

		GridData gridDataBanner = new GridData();
		gridDataBanner.horizontalAlignment = GridData.FILL;
		gridDataBanner.grabExcessHorizontalSpace = true;
		gridDataBanner.verticalAlignment = GridData.BEGINNING;
		gridDataBanner.heightHint = 60;
		GridLayout gridLayout = new GridLayout();
		gridLayout.marginHeight = 0;
		gridLayout.horizontalSpacing = 0;
		gridLayout.verticalSpacing = 0;
		gridLayout.marginWidth = 0;
		Monitor monitor = display.getPrimaryMonitor();
        Rectangle rect = monitor.getBounds();
		loginShell = new Shell(SWT.MIN | SWT.CLOSE);
		loginShell.setText(translator.getString("login.dialog.title"));
		loginShell.setImage(new Image(display, Environment.getFilePath("img/logo.png")));
		loginShell.setLayout(gridLayout);
		loginShell.setSize(new Point(WIDTH, HEIGHT));
		loginShell.setLocation((rect.width - WIDTH) / 2, (rect.height - HEIGHT) / 2);
		labelBanner = new Label(loginShell, SWT.SHADOW_NONE);
		labelBanner.setLayoutData(gridDataBanner);
		labelBanner.setImage(new Image(display, Environment.getFilePath("img/banner.png")));

		createGroup();
		createButtonComposite();
		loginShell.pack();
	}

	private void createGroup()
	{
		GridLayout gridLayoutGrp = new GridLayout();
		gridLayoutGrp.horizontalSpacing = 0;
		gridLayoutGrp.marginHeight = 0;
		gridLayoutGrp.marginWidth = 5;
		gridLayoutGrp.numColumns = 4;
		gridLayoutGrp.verticalSpacing = 0;
		GridData gridDataGrp = new GridData();
		gridDataGrp.grabExcessHorizontalSpace = true;
		gridDataGrp.horizontalAlignment = GridData.FILL;
		group = new Group(loginShell, SWT.NONE);
		group.setLayoutData(gridDataGrp);
		group.setLayout(gridLayoutGrp);
		group.setText(translator.getString("login.dialog.group"));

		labelUser = new Label(group, SWT.SHADOW_NONE);
		labelUser.setText(translator.getString("login.dialog.account"));
		labelUser.setLayoutData(new GridData());

		createComboUser();
		labelAt = new Label(group, SWT.SHADOW_NONE);
		labelAt.setLayoutData(new GridData());
		labelAt.setText("@");
		createComboDomain();

		labelPass = new Label(group, SWT.SHADOW_NONE);
		labelPass.setText(translator.getString("login.dialog.password"));
		labelPass.setLayoutData(new GridData());

		GridData gridDataTxt = new GridData();
		gridDataTxt.grabExcessHorizontalSpace = true;
		gridDataTxt.horizontalAlignment = GridData.FILL;
		gridDataTxt.horizontalIndent = 5;
		gridDataTxt.horizontalSpan = 3;
		textPass = new Text(group, SWT.BORDER);
		textPass.setEchoChar('*');
		textPass.setLayoutData(gridDataTxt);

		createOptionComposite();
	}

	private void createComboUser()
	{
		GridData gridDataUser = new GridData();
		gridDataUser.grabExcessHorizontalSpace = true;
		gridDataUser.horizontalAlignment = GridData.FILL;
		gridDataUser.horizontalIndent = 5;
		comboUser = new Combo(group, SWT.NONE);
		comboUser.setLayoutData(gridDataUser);
		//TODO	填充曾经记录的用户名
	}

	private void createComboDomain()
	{
		comboDomain = new Combo(group, SWT.NONE | SWT.READ_ONLY);
		comboDomain.setLayoutData(new GridData());
		String[] items = {"gmail.com"};
		comboDomain.setItems(items);
		comboDomain.setText("gmail.com");
	}

	private void createOptionComposite()
	{
		GridData gridDataCmp = new GridData();
		gridDataCmp.grabExcessHorizontalSpace = true;
		gridDataCmp.horizontalAlignment = GridData.FILL;
		gridDataCmp.horizontalSpan = 4;
		GridLayout gridLayoutCmp = new GridLayout();
		gridLayoutCmp.horizontalSpacing = 0;
		gridLayoutCmp.marginWidth = 0;
		gridLayoutCmp.verticalSpacing = 0;
		gridLayoutCmp.numColumns = 2;
		gridLayoutCmp.marginHeight = 0;
		optionComposite = new Composite(group, SWT.NONE);
		optionComposite.setLayout(gridLayoutCmp);
		optionComposite.setLayoutData(gridDataCmp);

		GridData gridDataCheck = new GridData();
		gridDataCheck.grabExcessHorizontalSpace = true;
		gridDataCheck.horizontalAlignment = GridData.FILL;
		checkRemember = new Button(optionComposite, SWT.CHECK);
		checkRemember.setText(translator.getString("login.dialog.remember"));
		checkRemember.setLayoutData(gridDataCheck);

		createComboStatus();
	}

	private void createComboStatus()
	{
		GridData gridData = new GridData();
		gridData.horizontalAlignment = GridData.END;
		comboStatus = new Combo(optionComposite, SWT.NONE | SWT.READ_ONLY);
		String[] items = {translator.getString("login.status.online"),
				translator.getString("login.status.busy"),
				translator.getString("login.status.offline")};
		comboStatus.setItems(items);
		comboStatus.setLayoutData(gridData);
		String status = Configuration.getDefaultStatus();
		if(status != null)
		{
			comboStatus.setText(translator.getString(status));
		}
		else
		{
			comboStatus.setText(translator.getString("login.status.online"));
		}
	}

	private void createButtonComposite()
	{
		GridLayout gridLayoutCmp = new GridLayout();
		gridLayoutCmp.horizontalSpacing = 0;
		gridLayoutCmp.marginWidth = 10;
		gridLayoutCmp.numColumns = 3;
		gridLayoutCmp.verticalSpacing = 0;
		gridLayoutCmp.marginHeight = 3;
		GridData gridDataCmp = new GridData();
		gridDataCmp.grabExcessHorizontalSpace = true;
		gridDataCmp.horizontalAlignment = GridData.FILL;
		buttonComposite = new Composite(loginShell, SWT.NONE);
		buttonComposite.setLayoutData(gridDataCmp);
		buttonComposite.setLayout(gridLayoutCmp);

		GridData gridDataLogin = new GridData();
		gridDataLogin.horizontalIndent = 0;
		buttonLogin = new Button(buttonComposite, SWT.NONE);
		buttonLogin.setLayoutData(gridDataLogin);
		buttonLogin.setText(translator.getString("login.dialog.button.login"));

		GridData gridDataExit = new GridData();
		gridDataExit.horizontalIndent = 5;
		buttonExit = new Button(buttonComposite, SWT.NONE);
		buttonExit.setLayoutData(gridDataExit);
		buttonExit.setText(translator.getString("login.dialog.button.exit"));

		GridData gridDataAdv = new GridData();
		gridDataAdv.grabExcessHorizontalSpace = true;
		gridDataAdv.horizontalAlignment = GridData.END;
		buttonAdvance = new Button(buttonComposite, SWT.NONE);
		buttonAdvance.setLayoutData(gridDataAdv);
		buttonAdvance.setText(translator.getString("login.dialog.button.advance"));

		buttonAdvance.addSelectionListener(new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent e)
			{
				LoginOptionDialog option = new LoginOptionDialog();
				option.show();
			}
		});
		buttonExit.addSelectionListener(new SelectionAdapter()
		{   
			public void widgetSelected(SelectionEvent e)
			{
				loginShell.close();
			}
		});
		buttonLogin.addSelectionListener(new SelectionAdapter()
		{
			public void widgetSelected(SelectionEvent e)
			{
				String val = comboStatus.getText();
				if(val.equals(translator.getString("login.status.online")))
				{
					Configuration.setDefaultStatus("login.status.online");
				}
				else if(val.equals(translator.getString("login.status.busy")))
				{
					Configuration.setDefaultStatus("login.status.busy");
				}
				else if(val.equals(translator.getString("login.status.offline")))
				{
					Configuration.setDefaultStatus("login.status.offline");
				}

				//TODO	执行登录动作
				String user = comboUser.getText();
				if(user == null || user.isEmpty())
				{
					user = System.getProperty("user.name");
				}
				account = user + "@" + comboDomain.getText();
				loginSuccess = true;
				loginShell.dispose();
			}
		});
	}

	public boolean isLoginSuccess()
	{
		return loginSuccess;
	}

	public String getAccount()
	{
		if(! loginSuccess)
		{
			return null;
		}

		return account;
	}
}
